package com.hugesoft.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import android.graphics.Bitmap;
import android.os.Environment;

import com.hugesoft.jxgs.JXGSApplication;

public class FileUtil {

	private static String FILEPATH = "/heme/smile/data/";

	// public static void writePbDataToFile(String filename,
	// com.google.protobuf.GeneratedMessage message, boolean append) {
	// String fullPath = getFullPathWithFileName(filename);
	// ensureFile(fullPath);
	// FileOutputStream output = null;
	// try {
	// output = new FileOutputStream(fullPath, append);
	// output.write(message.toByteArray());
	// output.close();
	// } catch (FileNotFoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } finally {
	//
	// }
	// }

	public static void writeStringToFile(String filename, String data,
			boolean append) {
		String fullPath = getFullPathWithFileName(filename);
		ensureFile(fullPath);
		FileWriter fw;
		try {
			fw = new FileWriter(fullPath, append);
			fw.write(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// public static LoginReq readLoginReqFromFile(String filename) {
	// String fullpath = getFullPathWithFileName(filename);
	// FileInputStream fs = readFsFromFilePath(fullpath);
	// if (fs != null) {
	// try {
	// return LoginReq.parseFrom(fs);
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// return null;
	// }
	// } else {
	// return null;
	// }
	// }

	// public static LoginRsp readLoginRspFromFile(String filename) {
	// String fullpath = getFullPathWithFileName(filename);
	// FileInputStream fs = readFsFromFilePath(fullpath);
	// if (fs != null) {
	// try {
	// return LoginRsp.parseFrom(fs);
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// return null;
	// }
	// } else {
	// return null;
	// }
	// }

	private static FileInputStream readFsFromFilePath(String path) {
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(path);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fs;
	}

	public static boolean isSDCardExist() {
		return Environment.MEDIA_MOUNTED.equalsIgnoreCase(Environment
				.getExternalStorageState());
	}

	private static boolean ensureFile(String path) {
		if (null == path) {
			return false;
		}

		boolean ret = false;

		File file = new File(path);
		if (!file.exists() || !file.isFile()) {
			try {
				file.createNewFile();
				ret = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	public static boolean ensureDir(String path) {
		if (null == path) {
			return false;
		}

		boolean ret = false;

		File file = new File(path);
		if (!file.exists() || !file.isDirectory()) {
			try {
				ret = file.mkdirs();
			} catch (SecurityException se) {
				se.printStackTrace();
			}
		}

		return ret;
	}

	public static String getFullAppDataPath() {
		StringBuffer sb = new StringBuffer();
		if (isSDCardExist()) {
			sb.append(Environment.getExternalStorageDirectory().getPath());
		} else {
			sb.append(JXGSApplication.APPPATH);
		}
		sb.append(FILEPATH);
		return sb.toString();
	}

	private static String getFullPathWithFileName(String filename) {
		StringBuffer sb = new StringBuffer();
		sb.append(getFullAppDataPath());
		sb.append(filename);
		return sb.toString();
	}

	public static boolean deleteFile(String filename) {
		String fullpath = getFullPathWithFileName(filename);
		File file = new File(fullpath);
		if (!file.exists() || !file.isFile()) {
			// 本就没有
			return true;
		} else {
			return file.delete();
		}
	}

	public static String saveBitmapToSdCard(Bitmap bitmap, String path) {
		FileOutputStream b = null;
		String name = System.currentTimeMillis() + ".jpg";
		String fileName = path + "/" + name;
		String savePath = null;
		try {
			b = new FileOutputStream(fileName);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				bitmap.recycle();
				bitmap = null;
				System.gc();
				b.flush();
				b.close();
				savePath = fileName;
			} catch (IOException e) {
				savePath = null;
				e.printStackTrace();
			}
		}
		return savePath;
	}
}
